package du.com.listview;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static du.com.listview.R.id.lv_subjects;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private ListView mListView;
    private SimpleAdapter msubjectAdapter;
    private List<Map<String,Object>> data;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initAdapt();
        listener();
    }

    private void listener() {
        mListView.setOnItemClickListener(this);
    }

    void initAdapt(){

        String[] from=new String[]{"subjectName","Status"};
        int[] to=new int[]{R.id.tv_subject,R.id.tv_isstatus};
        msubjectAdapter=new SimpleAdapter(this,initData(),R.layout.subject_item,from,to);
        mListView.setAdapter(msubjectAdapter);



    }

    //创建数据
    private  List<Map<String,Object>> initData(){
        String [] s=new String[]{"数学","英语","化学","语文"};
        data =new ArrayList<Map<String, Object>>();
        for(int i=0;i<s.length;i++){
            Map<String,Object> map =new HashMap<String,Object>();

            map.put("subjectName",s[i]);
            map.put("Status","2份未完成");
            data.add(map);

        }
        return data;

    }

    void initView(){
      mListView= (ListView) findViewById(lv_subjects);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {



        Intent intent=new Intent(this,HomeworkActivity.class);
        startActivity(intent);

    }
}
